<?php

namespace Association\Controller;

use Think\Controller;

class IndexController extends CommonController {

    public function index() {

        $masterid = cookie('masterid');
        
        $sql = M('shareSeller');

        if (IS_POST) {
           $mobile = I('post.mobile');
        }
        
        $whereSql = '';
        
        if(!empty($mobile)) {
            $whereSql = " and s.mobile = '".$mobile."'";
        }
        
        $count = $sql->where('masterid = '. $masterid)->count(); // 查询满足要求的总记录数
        $P = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)

        $tpl = '<ul data-am-widget="pagination" class="am-pagination am-pagination-default am-fr">';
        $tpl .= '<li class="am-pagination-first ">%FIRST%</li>';
        $tpl .= '<li class="am-pagination-prev ">%UP_PAGE%</li>';
        $tpl .= '<li class="">%LINK_PAGE%</li>';
        $tpl .= '<li class="am-pagination-next ">%DOWN_PAGE%</li>';
        $tpl .= '<li class="am-pagination-last ">%END%</li>';
        $tpl .= '</ul>';

        $P->setConfig('header', '条记录');
        $P->setConfig('first', '首页');
        $P->setConfig('prev', '上一页');
        $P->setConfig('next', '下一页');
        $P->setConfig('last', '末页');
        $P->setConfig('theme', $tpl);

        $show = $P->show(); // 分页显示输出

        $list = $sql->alias('s')->join('left join l_share_product p on p.sellerid = s.id')
                ->where('s.masterid = '. $masterid . $whereSql)->field('s.*,p.productid,p.brand')->limit($P->firstRow . ',' . $P->listRows)->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count);

        $this->display('./master-index');
    }

    function valid() {
        $sellerid = I('get.id');
        $seller = M('shareSeller');
        $data['status'] = 1;
        $seller->where('id='.$sellerid)->save($data);
        header("Location: /Association");
    }
    
    function setting() {
        
        $masterid = cookie('masterid');
        
        if (IS_POST) {
        
            $address = I('post.address');
            $starttime = I('post.starttime');
            
            $masterdata = M('master');
            $data['address'] = $address;
            $data['starttime'] = $starttime;
            $masterdata->where('id='.$masterid)->save($data);
            
            header("Location: /Association/index/setting");
            
        }
        
        $master = M('master')->where('id='.$masterid)->find();

        $this->assign('master', $master);
        
        $this->display('./master-setting');
    }
    
    function product() {
        
        $masterid = cookie('masterid');
        
        $master = M('master')->where('id='.$masterid)->find();

        $product = M('shareProduct')->alias('p')->join('inner join l_share_seller s on s.id = p.sellerid')
                                    ->join('inner join l_master m on m.id = s.masterid')->where('m.id = '.$masterid)
                                    ->field('p.*,s.status,s.truename,s.mobile')->select();
        
        $count = count($product);
        $P = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)

        $tpl = '<ul data-am-widget="pagination" class="am-pagination am-pagination-default am-fr">';
        $tpl .= '<li class="am-pagination-first ">%FIRST%</li>';
        $tpl .= '<li class="am-pagination-prev ">%UP_PAGE%</li>';
        $tpl .= '<li class="">%LINK_PAGE%</li>';
        $tpl .= '<li class="am-pagination-next ">%DOWN_PAGE%</li>';
        $tpl .= '<li class="am-pagination-last ">%END%</li>';
        $tpl .= '</ul>';

        $P->setConfig('header', '条记录');
        $P->setConfig('first', '首页');
        $P->setConfig('prev', '上一页');
        $P->setConfig('next', '下一页');
        $P->setConfig('last', '末页');
        $P->setConfig('theme', $tpl);

        $show = $P->show(); // 分页显示输出
        
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count);
        
        $this->assign('product', $product);
        $this->assign('master', $master);
        
        $this->display('./master-product');
    }
    
}
